import { ajax } from '@/utils'

import {
  GetCodeListRes,
  GetCouponDetailRes,
  GetCouponListRes,
  GetExchangeListRes,
  GetSimpleListReq,
  GetSimpleListRes,
  UploadTemplateRes
} from './couponTypes'

const baseUrl = '/yswy-zy-coupon-manage'

export default {
  /** 分页查询优惠券 */
  getList(data): Promise<GetCouponListRes> {
    return ajax.post(`${baseUrl}/couponManage/list`, data)
  },
  /** 删除单个优惠券 */
  deleteOne(id: number): Promise<void> {
    return ajax.delete(`${baseUrl}/couponManage/del/${id}`)
  },
  /** 删除多个优惠券 */
  deleteList(ids: number[]) {
    return ajax.delete(`${baseUrl}/couponManage/batchDel`, {
      data: { ids }
    })
  },
  /** 创建优惠券 */
  createOne(data): Promise<void> {
    return ajax.post(`${baseUrl}/couponManage/create`, data)
  },
  /** 获取优惠券详情 */
  getDetail(id: number): Promise<GetCouponDetailRes> {
    return ajax.post(`${baseUrl}/couponManage/detail/${id}`)
  },
  /** 更新优惠券 */
  updateOne(data) {
    return ajax.put(`${baseUrl}/couponManage/edit`, data)
  },
  /** 导出模板 */
  exportTemplate() {
    return ajax.get(`${baseUrl}/couponManage/exportModel`, {
      responseType: 'blob',
      withResponse: true
    })
  },
  /** 上传模板 */
  uploadTemplate(data): Promise<UploadTemplateRes> {
    return ajax.post(`${baseUrl}/couponManage/upload`, data, {
      timeout: 600_000
    })
  },
  /** 分页查询码库 */
  getCodeList(data): Promise<GetCodeListRes> {
    return ajax.post(`${baseUrl}/couponWarehouse/list`, data)
  },
  /** 导出码库 */
  exportCodeList(data) {
    return ajax.post(`${baseUrl}/couponWarehouse/export`, data, {
      withResponse: true,
      responseType: 'blob'
    })
  },
  /** 作废多个码库 */
  invalidCodeList(ids: number[]) {
    return ajax.put(`${baseUrl}/couponWarehouse/batchCancel`, { ids })
  },
  /** 获取兑换活动列表 */
  getExchangeList(): Promise<GetExchangeListRes> {
    return ajax.get(`${baseUrl}/couponManage/exchangeList`)
  },
  /** 获取简单列表 */
  getSimpleList(data: GetSimpleListReq): Promise<GetSimpleListRes> {
    return ajax.get(`${baseUrl}/couponManage/simpleList`, { params: data })
  }
}
